<?php

require_once 'Pipes/FileSystem/File/Finder.php';
require_once 'PipesImpl/Config.php';

class Pipes_FileSystem_View_Finder
{
    private $_projects = array();
    private $_path = '';

    public function __construct(PipesImpl_Config $config, $path) {
        assert('is_string($path)');

        $this->_projects = array_reverse($config->projects);
        $this->_path = $path;
    }

    public function find($name) {
        $name = "{$this->_path}/{$name}.php";
        $fileFinder = new Pipes_FileSystem_File_Finder();
        foreach ($this->_projects as $project) {
            try {
                $file = $fileFinder->find("{$project}/{$name}");
                return $file;
            } catch (Pipes_Exception $e) {
                // try the next namespace
                $file = false;
            }
        }

        throw new Pipes_Exception("Unable to locate file for [{$name}] in any project");
    }
}
